﻿using ICSharpCode.WinFormsUI.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core
{
    public partial class StartForm : Form
    {
        #region 动画窗体调用
        //动画窗体调用
        [DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        const int AW_HOR_POSITIVE = 0x0001;
        const int AW_HOR_NEGATIVE = 0x0002;
        const int AW_VER_POSITIVE = 0x0004;
        const int AW_VER_NEGATIVE = 0x0008;
        const int AW_CENTER = 0x0010;
        const int AW_HIDE = 0x10000;
        const int AW_ACTIVATE = 0x20000;
        const int AW_SLIDE = 0x40000;
        const int AW_BLEND = 0x80000;
        //动画窗体调用
        #endregion

        int radius = 2;

        public StartForm()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            //this.Radius = 2;
            //this.ShowShadow = false;
            this.ControlBox = false;
            //this.CaptionHeight = 0;           
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.FormBorderStyle = FormBorderStyle.None;      
        }

        private void StartForm_Load(object sender, EventArgs e)
        {
            AnimateWindow(this.Handle, 300, AW_HOR_NEGATIVE | AW_CENTER);
            backgroundWorker1.RunWorkerAsync();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            int delay = 4 * 1000;
            System.Threading.Thread.Sleep(delay);
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void StartForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            AnimateWindow(this.Handle, 200, AW_CENTER + AW_VER_NEGATIVE + AW_HIDE);
        }

        private void SetWindowRegion()
        {
            //Rectangle rect = new Rectangle(0, radius, this.Width, this.Height - 2 * radius);
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            System.Drawing.Drawing2D.GraphicsPath FormPath = GetRoundedRectPath(rect, (int)(radius * 1.0));
            this.Region = new Region(FormPath);
        }

        protected override void OnResize(System.EventArgs e)
        {
            SetWindowRegion();
        }

        private GraphicsPath GetRoundedRectPath(Rectangle rect, int radius)
        {
            int diameter = radius;
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            //   左上角  
            path.AddArc(arcRect, 180, 90);
            //   右上角   
            arcRect.X = rect.Right - diameter;
            path.AddArc(arcRect, 270, 90);
            //   右下角  
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);
            //   左下角   
            arcRect.X = rect.Left;
            path.AddArc(arcRect, 90, 90);
            path.CloseFigure();
            return path;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            System.Drawing.Drawing2D.GraphicsPath FormPath = GetRoundedRectPath(rect, (int)(radius * 1.0));
            Graphics graphic = e.Graphics;
            graphic.DrawPath(new Pen(new SolidBrush(_xtheme.FormBorderOutterColor), 2.0f), FormPath);
        }

        private ICSharpCode.WinFormsUI.Theme.WinFormsUIThemeBase _xtheme;
        public ICSharpCode.WinFormsUI.Theme.WinFormsUIThemeBase XTheme
        {
            set
            {
                if (value != null)
                    _xtheme = value.CloneThemeBase();                
                _xtheme.ShowShadow = false;                
            }
            get { return _xtheme; }
        }
        
    }
}
